package com.company.serialization;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.msgpack.MessagePack;

import java.util.List;

/**
 * @description: msgpack解码器
 * @author: ruoan
 * @date: 2021/1/1 21:40
 */
public class MsgpackDecoder extends MessageToMessageDecoder<ByteBuf> {
    /**
     * ByteBuf的 byte[] 转化为 Object
     * @param ctx
     * @param byteBuf
     * @param list
     * @throws Exception
     */
    @Override
    protected void decode(ChannelHandlerContext ctx,
                          ByteBuf byteBuf, List<Object> list) throws Exception {
        final byte[] buf = new byte[byteBuf.readableBytes()];
        byteBuf.getBytes(byteBuf.readerIndex(), buf, 0, buf.length);
        MessagePack messagePack = new MessagePack();
        list.add(messagePack.read(buf));

    }
}
